/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller.bean;

import com.entites.EventRegistration;
import com.model.bean.EventRegistrationFacadeLocal;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author VANANH
 */
@Named(value = "eventRegistrationController")
@SessionScoped
public class EventRegistrationController implements Serializable
{

    @EJB
    private EventRegistrationFacadeLocal eventRegistrationFacadeLocal;
    private String username;
    private int pages;
    private int currentPages;
    private int noOfRecords;
    private int noOfPage;
    private int eventId;

    FacesContext fCtx = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(true);
    
    public EventRegistrationFacadeLocal getEventRegistrationFacadeLocal()
    {
        return eventRegistrationFacadeLocal;
    }

    public void setEventRegistrationFacadeLocal(EventRegistrationFacadeLocal eventRegistrationFacadeLocal)
    {
        this.eventRegistrationFacadeLocal = eventRegistrationFacadeLocal;
    }

    public int getPages()
    {
        return pages;
    }

    public void setPages(int pages)
    {
        this.pages = pages;
    }

    public int getCurrentPages()
    {
        return currentPages;
    }

    public void setCurrentPages(int currentPages)
    {
        this.currentPages = currentPages;
    }

    public int getNoOfRecords()
    {
        return noOfRecords;
    }

    public void setNoOfRecords(int noOfRecords)
    {
        this.noOfRecords = noOfRecords;
    }

    public int getNoOfPage()
    {
        return noOfPage;
    }

    public void setNoOfPage(int noOfPage)
    {
        this.noOfPage = noOfPage;
    }

    public int getEventId()
    {
        return eventId;
    }

    public void setEventId(int eventId)
    {
        this.eventId = eventId;
    }
    
     public String getUsername() {
        return username = (String) session.getAttribute("username");
    }

    /**
     * Creates a new instance of EventRegistrationController
     */
    public EventRegistrationController()
    {
    }

    public List<EventRegistration> getListEventRegistration()
    {
        List<EventRegistration> lst = new ArrayList<>();
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if (request.getParameter("pages") != null)
        {
            pages = Integer.parseInt(request.getParameter("pages"));
        }
        int record = 6;
        currentPages = 1;
        if (pages != 0)
        {
            currentPages = pages;
        }
        int start_row = (currentPages * record) - (record - 1);
        int end_row = start_row + record - 1;
        lst = eventRegistrationFacadeLocal.getEventRegistedByUser(start_row, end_row, getUsername());
        noOfRecords = eventRegistrationFacadeLocal.count();
        noOfPage = ((int) Math.ceil(noOfRecords * 1.0 / record));
        return lst;
    }

}
